package generic.spread;

import generic.Publisher;
import java.net.*;
import java.io.IOException;
import spread.*;

public class SpreadPublisher implements Publisher {
	private SpreadConnection connection = new SpreadConnection();
	private SpreadMessage spreadMsg = new SpreadMessage();
	public SpreadPublisher() {
		super();
		try{
			InetAddress addr = InetAddress.getByName("localhost");
			
			System.out.println("Connecting to daemon :" + addr  );
			connection.connect( addr , 0, "Publisher", false, false);
		
			spreadMsg.addGroup("group");
			//spreadMsg.setReliable();
			spreadMsg.setUnreliable();
			
			System.out.println("SpreadPublisher ready to publish messages...");
		}
		catch( Exception e )
		{
			e.printStackTrace();
		}
	}
	
	@Override
	public boolean send(String message) {
		boolean result = false;
		try {
			spreadMsg.setObject(message);
			connection.multicast(spreadMsg);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}
